import csv
import datetime
import os
import time

import requests
from lxml import etree

list = []


class Xin(object):
    def __init__(self):

        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"
        }
        cookies ="pif=; RELEASE_KEY=; XIN_anti_uid=763EAD21-778C-41D4-1765-66A2CC8AC73C; XIN_LOCATION_CITY=%7B%22cityid%22%3A%221520%22%2C%22cityname%22%3A%22%5Cu5bbf%5Cu8fc1%22%2C%22ename%22%3A%22suqian%22%2C%22service%22%3A%221%22%2C%22zhigou%22%3A%221%22%2C%22is_visit%22%3A%221%22%2C%22city_rank%22%3A%22100%22%2C%22is_gold_partner%22%3A%22-1%22%2C%22direct_rent_support%22%3A%221%22%2C%22is_wz_mortgage%22%3A%221%22%7D; uid=CvQ2IV/RbpYdUQAVE7LaAg==; XIN_UID_CK=51f51f8b-72d8-3baf-1473-598600a1faa6; Hm_lvt_ae57612a280420ca44598b857c8a9712=1607560860; SEO_SOURCE=https://www.xin.com/suqian/i1/; SEO_REF=https://www.xin.com/suqian/i1/; acw_tc=1bdd5d9d16075806322758413ea347c37a3c0062952868f88c37875bd9; acw_sc__v2=5fd1bbe8c5a996e2b0e455e68999f79897aeddd5; session_xin=599jtfj9ahf63h28sqqmlfos4cq7efqo; Hm_lpvt_ae57612a280420ca44598b857c8a9712=1607580649; SERVERID=b13028cd19ef1711d5f40612dd61793a|1607580651|1607578743"
        self.cookies_dict = {temp[:temp.find("=")]: temp[temp.find("=") + 1:] for temp in cookies.split("; ")}
        self.list = []

    def main(self, url):
        """
        爬取数据
        :param url: 爬取的地址
        :return:
        """
        self.r = requests.post(url=url, headers=self.headers, cookies=self.cookies_dict)
        #
        self.html = etree.HTML(self.r.text)
        # print(self.r.text)
        info_list = self.html.xpath('//ul/li[@class="con caritem conHeight"]')
        print("提取到的个数：", len(info_list))
        for info in info_list:
            item = {}
            item['title'] = info.xpath('.//div[@class="pad"]/h2/span/text()')[0]
            url = "http:" + info.xpath('./@data-img')[0]
            item['img'] = url
            time = info.xpath('.//div[@class="pad"]/span/text()')[0]
            time_str = time.strip()
            item['time'] = time_str[0:-1]
            data = datetime.datetime.now()
            item['data'] = '%s年%s月%s日' % (data.year, data.month, data.day)
            # print(item)
            price = info.xpath('.//em/text()')[0]
            price_str = price.strip()
            # print(price_str[0:6])
            price_text = price_str.replace('\n                                                            万', "")
            item['price'] = price_text
            # print(item)
            self.list.append(item)

    def write(self):
        """
        把爬取到的数据写入对应的文件里
        :return:
        """
        download_path = os.getcwd() + '\优信二手车'
        if not os.path.exists(download_path):
            os.makedirs(download_path)
        for item in self.list:
            title = item.get('title')
            img_url = item.get('img')
            temp_list = title.split(" ")
            download_path_pd = os.getcwd() + "\优信二手车/%s/images" % temp_list[0]
            if not os.path.exists(download_path_pd):
                os.makedirs(download_path_pd)
            img = requests.get(url=url, headers=self.headers)

            print("图片爬取中！")
            time.sleep(2)
            # 将图片写入对应的路径中
            with open("./优信二手车/%s/images/%s.jpg" % (temp_list[0], title), "wb") as f:
                f.write(img.content)
            #
            with open("./优信二手车/%s/%s.csv" % (temp_list[0], temp_list[1]), "a", encoding="utf-8") as f:
                f_csv = csv.DictWriter(f, ['title', 'img', 'time', 'data','price'])
                f_csv.writeheader()
                f_csv.writerows([item])
                print("保存信息到CSV....ok")

if __name__ == '__main__':
    xin = Xin()
    # 爬取五页数据
    for i in range(1,6):
        url = 'https://www.xin.com/suqian/i%s/' % (i)
        xin.main(url)
    xin.write()
